//
//  FYNetworkTools.swift
//  Weibo
//
//  Created by 冯毅 on 16/8/9.
//  Copyright © 2016年 冯毅. All rights reserved.
//

import UIKit
import AFNetworking

//网络请求的枚举类型
enum RequestType: Int {
    case GET = 0
    case POST = 1
}

class FYNetworkTools: AFHTTPSessionManager {
    
    static let sharedTools: FYNetworkTools = {
        let tools = FYNetworkTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()
    
    func request(requestType: RequestType, url: String, params: AnyObject?, callBack:(response: AnyObject?, error: NSError?)->()) {
        
        if requestType == .GET {
            
            GET(url, parameters: params, progress: nil, success: { (_,response) -> Void in
                    callBack(response: response, error: nil)
                
                }, failure: { (_,error) -> Void in
                    callBack(response: nil, error: error)
            })
            
        } else {
            
            POST(url, parameters: params, progress: nil, success: { (_, response) -> Void in
                callBack(response: response, error: nil)
                }, failure: { (_, error) -> Void in
                    callBack(response: nil, error: error)
            })
        }
        
    }

}

extension FYNetworkTools {
    //获取当前登录用户及其所关注(授权)用户的最新微博
    func requestStatuses(accessToken: String, maxId: Int64, sinceId: Int64, callBack: (response: AnyObject?, error:NSError?)->()) {
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let params = [
            "access_token": accessToken,
            "max_id": "\(maxId)",
            "since_id": "\(sinceId)"
        ]
        
        let path = url + "?access_token=" + accessToken
        
        print(path)
        
        request(.GET, url: url, params: params, callBack: callBack)
    }
    
}

extension FYNetworkTools {

    func reqeustAccessTokenWithCode(code: String, callBack: (response: AnyObject?, error: NSError?)->() ){
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let params = [
            "client_id": WeiboAppKey,
            "client_secret": WeiboAppSecret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WeiboRedirect_Uri
        ]
        
        request(.POST, url: url, params: params, callBack: callBack)
        
    }
    
    //通过accessToken和uid获取用户信息
    func requestUserInfo(userAccount: FYUserAccount, callBack: (response: AnyObject?, error: NSError?)->()) {
        let url = "https://api.weibo.com/2/users/show.json"
        
        let params = [
            "access_token": userAccount.access_token!,
            "uid": "\(userAccount.uid)"
        ]
        
        request(.GET, url: url, params: params, callBack: callBack)
    }
    
}

extension FYNetworkTools {
    func update(access_token: String, status: String, callBack:(response: AnyObject?, error: NSError?)->()){
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            "access_token": access_token,
            "status":status
        ]
        request(.POST, url: url, params: params, callBack: callBack)
    }
}




